
dofile("Sec_clean.lua")

local function totime(value)
	if value > 3600 then
		local h, m, s
		h = math.floor(value / 3600)
		value = value - h * 3600
		m = math.floor(value / 60)
		s = math.floor(value - m * 60 + 0.5)
		return string.format("%dh %dm %ds", h, m, s)
	elseif value > 60 then
		local m, s
		m = math.floor(value / 60)
		s = math.floor(value - m * 60 + 0.5)
		return string.format("%dm %ds", m, s)
	else
		return string.format("%ds", math.floor(value + 0.5))
	end
end

local function get_attendance(events, out)
	local players = {}
	for _, event in ipairs(events) do
		if event.type == "enter" then
			local state = players[event.player]
			if not state then
				state = { active = 0, afk = 0, offline = 0 }
				players[event.player] = state
			end
			if state.state ~= "active" then
				state.state = "active"
				state.last_time = event.time
			end
		elseif event.type == "leave" then
			local state = players[event.player]
			assert(state)
			if state.state ~= "left" then
				local delta_time = event.time - state .last_time
				state.last_time = event.time
				state[state.state] = state[state.state] + delta_time
				state.state = "left"
			end
		elseif event.type == "afk" or event.type == "offline" then
			local state = players[event.player]
			assert(state)
			local delta_time = event.time - state .last_time
			state.last_time = event.time
			assert(state.state == "active")
			state[state.state] = state[state.state] + delta_time
			state.state = event.type
		elseif event.type == "return" then
			local state = players[event.player]
			assert(state)
			local delta_time = event.time - state .last_time
			state.last_time = event.time
			assert(state.state ~= "active" and state.state ~= "left")
			state[state.state] = state[state.state] + delta_time
			state.state = "active"
		end
	end
	out:write("[size=14]Attendance[/size]\n")
	out:write("[list]\n")
	out:write("[*][b]Name | Active | AFK | Offline[/b]\n")
	for name, info in pairs(players) do
		out:write(string.format("[*] %s | (%s) | (%s) | (%s)\n", name, totime(info.active), totime(info.afk), totime(info.offline)))
	end
	out:write("[/list]\n")
end

local function get_events(events)
	for _, event in ipairs(events) do
		if event.type == "loot" then
			print(event.time, event.type, event.name, event.count, event.item)
		elseif event.type == "kill" then
			print(event.time, event.type, event.name)
		elseif event.type == "wipe" then
			print(event.time, event.type)
		end
	end
end

local function build_item_link(itemlink)
	local color, id, name = itemlink:match("|cff([0-9a-f]+)|Hitem:(%d+):[^[]*(%[[^%]]+%])")
	return string.format("[color=#%s][url=http://armory.wow-europe.com/item-info.xml?i=%s]%s[/url][/color]", color, id, name)
end

local function get_loots(events, out)
	out:write("[size=14]Loots[/size]\n")
	out:write("[list]\n")
	out:write("[*][b]Time | Player | Amount | Itemlink[/b]\n")
	for _, event in ipairs(events) do
		if event.type == "loot" then
			out:write(string.format("[*] %s | %s | %s | %s\n", event.time, event.name, event.count, build_item_link(event.item)))
		end
	end
	out:write("[/list]\n")
end	

local function get_kills(events, out)
	out:write("[size=14]Kills[/size]\n")
	out:write("[list]\n")
	out:write("[*][b]Time | Boss[/b]\n")
	for _, event in ipairs(events) do
		if event.type == "kill" then
			out:write(string.format("[*] %s | %s\n", event.time, event.name))
		end
	end
	out:write("[/list]\n")
end	

output = io.stdout
output:write("[size=18]Secretary offline report[/size]\n")
get_attendance(SecretaryDB.profiles.Default.events, output)
-- get_events(SecretaryDB.profiles.Default.events)
get_loots(SecretaryDB.profiles.Default.events, output)
get_kills(SecretaryDB.profiles.Default.events, output)
output:close()
